
/*
* 修改手机  输入新手机操作
* */
var newPhoneDevice={
    /*android设备*/
    androidLoad:function(){
        var accounts=JSON.parse(AnroidWebActivity.getAuthorityState()).account;
        var token=AnroidWebActivity.getToken();
        newPhoneDevice.modifyPhone(accounts,token);
    },
    /*ios设备*/
    iosLoad:function(){
        var oldAccount=localStorage.getItem('oldTelephone');
        baseAll.runWebViewJavascriptBridgeArr(function(data,responseCallback){
            var dataContent={};
            //获取token账号-------
            WebViewJavascriptBridge.callHandler("getToken",null, function (response) {
                dataContent.token=response;
            });
            //获取serverID---------
            WebViewJavascriptBridge.callHandler("getServer",null, function (response) {
                localStorage.setItem('serverId',response.serverId);//储存serverID
            });
            //获取公钥与秘钥加密后返回的加密内容----
            newPhoneDevice.modifyPhone(oldAccount,dataContent.token,function(password,newPhone,newShortCode){
                //传输回来的str
                WebViewJavascriptBridge.callHandler("encStr",{'str':password}, function (response) {
                    dataContent.encPaswWord=response;
                    newPhoneDevice.sendAjax(newPhone,newShortCode,dataContent.encPaswWord,dataContent.token);
                });
            })
        });
    },
    /*修改进入新手机 ------*/
    /*
       *  @param accounts  旧手机账号
       *  @param token     token值
       *  @param callback  如果是IOS端的话就执行回调函数
    * */
    modifyPhone:function(accounts,token,callback){
        AlterMobilePhone.init({
            required_register:false,
            formInput:".formInput",
            mobileText:"#mobile",//获取手机文本框
            passText:".pass_mode",
            getCodeBtn:"#get_code",//获取验证码按钮
            codeText:"#codes",//短信验证码
            nextBtn:"#Confirm_bind",
            passEye:".pass_eye",//密码眼睛
            smsTPL:oPublic.OWNER_CHANGE_MOBILE_GPS,//短信模板编码
            model:{ //模态框
                modal:"#alterMobileModal",
                sureBtn:".sureBtn",
                cancelBtn:".closed_btn",
                codeImg:"#codeImg",//验证码图片
                imgTextBox:"#random_code"//图片随机验证码
            },
            //提交按钮判断显示
            statusSubBtn:function(formInput,mobileText,codeText,subBtn,passText){
                $(formInput).keyup(function(){
                    var telephone=$(mobileText).val();
                    var shortCodes=$(codeText).val();
                    var password=$(passText).val();
                    if(shortCodes.length==4){
                        if(telephone.length==11&&password.length>=8&&password.length<=16){
                            $(subBtn).attr("disabled",false).addClass("active_status");
                        }else{
                            $(subBtn).attr("disabled",true).removeClass("active_status");
                        }
                    }else{
                        $(subBtn).attr("disabled",true).removeClass("active_status");
                    }
                });
            },//进入下一步
            nextOperate:function(formInput,subBtn){
                var self=this;
                $(subBtn).on("click",function(){
                    var flag=true;
                    $(formInput).each(function(){
                        var regStr=$(this).attr("data-reg");
                        var errorMsg=$(this).attr("data-error");
                        var reg=new RegExp(regStr);
                        var oValue=$(this).val();
                        if(reg&&!reg.test(oValue)){
                            oPublic.errorText($(this),errorMsg,"warn","dl");
                            flag=false;
                            return;
                        }
                    });
                    if(flag){
                        var password=$(self.passText).val();//密码
                        var newPhone=$(self.mobileText).val();//新手机号码
                        var newShortCode=$(self.codeText).val();//旧短信验证码
                        //Android--  判断
                        if(baseAll.isMobileDevice()=='android'){
                            /*
                             *   accounts  账号    public_key   公钥  mi_key   秘钥
                            */
                            oPublic.getEcyPass(accounts,function(public_key,mi_key){
                                var encPaswWord=RSAUtils.encryptByPublicKey(public_key,mi_key,password);
                                //发送Ajax请求--------
                                newPhoneDevice.sendAjax(newPhone,newShortCode,encPaswWord,token);
                            });
                        }else if(baseAll.isMobileDevice()=='ios'){
                            //IOS端判断方式-----
                            callback&&callback(password,newPhone,newShortCode);
                        }else{
                            baseAll.pubilcModal('该APP只能支持Anroid和IOS');
                        }
                    }
                });
            }
        });
    },
    /*
      @param  newPhone      新手机号码
      @param  newShortCode  新短信验证码
      @param  encPassWord   加密的密码
    * @param  token  android/ios
    * */
    sendAjax:function(newPhone,newShortCode,encPassWord,token){
        baseAll.addLoading();
        $.ajax({
            type:"get",
            url:oPublic.basePath()+"/s/bike/owner/change/mobile",
            timeout:baseAll.timeout,
            data:{
                newMobile:newPhone,
                oldMobile:localStorage.getItem("oldTelephone"),
                password:encPassWord,
                isWechat:baseAll.isMobileDevice()=='ios'?"YES":"NO",
                oldValidateCode:localStorage.getItem("oldCode"),
                newValidateCode:newShortCode,
                serverId:localStorage.getItem("serverId"),
                token:token
            },
            success:function(data){
                if(data.opStatus==0){
                    baseAll.closeLoading();
                    baseAll.pubilcModal("修改手机号成功","确定",function(){
                        baseAll.closePageDevice();//页面关闭
                    });
                }else{
                    baseAll.closeLoading();
                    oPublic.pubilcModal(data.msgDesc);
                }
            },
            error:function(){
                baseAll.closeLoading();
                oPublic.pubilcModal("网络连接失败.....");
            }
        })
    }
}